package com.java.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java.dao.ActivityCoachDao;
import com.java.domain.ActivityCoach;
import com.java.domain.event.ActivityCoachEvent;
import com.java.service.CoachService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class CoachServiceImpl implements CoachService {

	@Resource ActivityCoachDao coachDao;
	
	@Override
	public TableRespone getList(int page, int size, Integer status) {
		Map<String, Object> params = new HashMap<String, Object>();
		if(!MyTools.isEmpty(status)) {
			params.put("coachStatus", status);
		}
		List<ActivityCoachEvent> list = coachDao.selectAll(params, page, size);
		long total = coachDao.selectAllCount(params);
		return new TableRespone(list, total);
	}

	@Override
	public ActivityCoachEvent getOne(String coachId) {
		ActivityCoachEvent info = coachDao.selectByKey(coachId);
		return info;
	}

	@Override
	public BaseRespone saveCoach(ActivityCoach coach) {
		int row = 0;
		ActivityCoach info = coachDao.selectByKey(coach.getCoachId());
		if(null == info) {
			info = new ActivityCoach();
			info.setCoachId(MyTools.isEmpty(coach.getCoachId())?MyTools.getUUID():coach.getCoachId())
			.setCoachStatus(1)
			.setCoachTime(new Date())
			.setCoachName(coach.getCoachName())
			.setCoachMobile(coach.getCoachMobile())
			.setCoachAvatar(coach.getCoachAvatar());
			row = coachDao.insertSelective(info);
		}else {
			info.setCoachName(coach.getCoachName())
			.setCoachMobile(coach.getCoachMobile())
			.setCoachAvatar(coach.getCoachAvatar());
			row = coachDao.updateSelective(info);
		}
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone departCoach(String coachId) {
		ActivityCoach info = coachDao.selectByKey(coachId);
		info.setCoachStatus(0);
		int row = coachDao.updateSelective(info);
		return BaseRespone.result(row, "Success");
	}

	@Override
	public void delCoachForTest(String coachId) {
		coachDao.deleteByKey(coachId);
	}

}
